/**
    Copyright (c) 2013, Robert Cherry    
    
    All rights reserved.
  
    This file is part of the Faust Editor.

    The Faust Editor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Faust Editor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with The Faust Editor.  If not, see <http://www.gnu.org/licenses/>.
*/
package form;

import core.world.WorldItem;
import io.resource.ResourceDelegate;
import java.awt.Color;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;

/**
 *
 * @author mcherry
 */
public class ItemSelector extends javax.swing.JDialog {

    // Variable Declaration
    private FaustEditor editor;
    private ResourceDelegate delegate;
    // Project Classes
    private WorldItem outputItem;
    // Data Inst.
    private double dropChance = 50;
    private String itemChosen;
    // End of Variable Declaration

    public ItemSelector(FaustEditor parent, WorldItem inputItem, boolean modal) {

        // Call to super
        super(parent, modal);

        // --
        editor = parent;
        //
        outputItem = inputItem;

        // Initialize component
        initComponents();

        // Initialize code
        init();
    }

    private void init() {

        // Creating a new list model
        DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
        DefaultListModel listModel = new DefaultListModel();

        // Fill the list model
//        for (Map.Entry<String, Item> set : delegate.getItems().entrySet()) {
//            listModel.addElement(set.getKey());
//        }

        // Fill the combo model
        //for (int i = 0; i < Item.ITEM_TYPES.length; i++) {
        //    comboModel.addElement(Item.ITEM_TYPES[i]);
        //}

        // Setup JCombobox
        typeJComboBox.setModel(comboModel);

        //
        chanceJSlider.setValue(new Integer((int) dropChance));
        chanceJTextField.setText(chanceJSlider.getValue() + "%");

        // Setup JList
        itemJList.setModel(listModel);
        itemJList.revalidate();
    }

    public double getChance() {
        return dropChance;
    }

    public WorldItem getItem() {
        return outputItem;
    }

    public void setItem(WorldItem newItem) {

        //
        outputItem = newItem;

        //
        if (outputItem != null) {
            itemJList.setSelectedValue(newItem, true);
        }
    }

    public void setChance(double newChance) {
        if (newChance < 0 || newChance > 100) {
            newChance = 50;
        }

        //
        dropChance = newChance;

        //
        chanceJSlider.setValue((int) dropChance);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonJPanel = new javax.swing.JPanel();
        chanceJTextField = new javax.swing.JTextField();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(8, 0), new java.awt.Dimension(8, 0), new java.awt.Dimension(8, 32767));
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        selectJButton = new javax.swing.JButton();
        filler4 = new javax.swing.Box.Filler(new java.awt.Dimension(8, 0), new java.awt.Dimension(8, 0), new java.awt.Dimension(8, 32767));
        closeJButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        itemJList = new javax.swing.JList();
        settingsJPanel = new javax.swing.JPanel();
        categoryJLabel = new javax.swing.JLabel();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        typeJComboBox = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        chanceJSlider = new javax.swing.JSlider();
        chanceJLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Item Selection");

        buttonJPanel.setMaximumSize(new java.awt.Dimension(280, 22));
        buttonJPanel.setMinimumSize(new java.awt.Dimension(280, 22));
        buttonJPanel.setPreferredSize(new java.awt.Dimension(280, 22));
        buttonJPanel.setLayout(new javax.swing.BoxLayout(buttonJPanel, javax.swing.BoxLayout.LINE_AXIS));

        chanceJTextField.setEditable(false);
        buttonJPanel.add(chanceJTextField);
        buttonJPanel.add(filler1);
        buttonJPanel.add(filler3);

        selectJButton.setText("Select");
        selectJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectJButtonActionPerformed(evt);
            }
        });
        buttonJPanel.add(selectJButton);
        buttonJPanel.add(filler4);

        closeJButton.setText("Close");
        closeJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeJButtonActionPerformed(evt);
            }
        });
        buttonJPanel.add(closeJButton);

        itemJList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        itemJList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                itemJListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(itemJList);

        settingsJPanel.setMaximumSize(new java.awt.Dimension(280, 22));
        settingsJPanel.setMinimumSize(new java.awt.Dimension(280, 22));
        settingsJPanel.setPreferredSize(new java.awt.Dimension(280, 22));
        settingsJPanel.setLayout(new javax.swing.BoxLayout(settingsJPanel, javax.swing.BoxLayout.LINE_AXIS));

        categoryJLabel.setText("Item Category:");
        settingsJPanel.add(categoryJLabel);
        settingsJPanel.add(filler2);

        typeJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        typeJComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeJComboBoxActionPerformed(evt);
            }
        });
        settingsJPanel.add(typeJComboBox);

        java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();
        jPanel1Layout.columnWidths = new int[] {0, 5, 0};
        jPanel1Layout.rowHeights = new int[] {0};
        jPanel1.setLayout(jPanel1Layout);

        chanceJSlider.setMajorTickSpacing(10);
        chanceJSlider.setPaintTicks(true);
        chanceJSlider.setToolTipText("The chance for this item to drop upon death of the actor; 100.0 is an assured drop while 0.0 is undropable");
        chanceJSlider.setPreferredSize(new java.awt.Dimension(168, 31));
        chanceJSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                chanceJSliderStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(chanceJSlider, gridBagConstraints);

        chanceJLabel.setText("Drop Chance:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        jPanel1.add(chanceJLabel, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                    .addComponent(settingsJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addComponent(buttonJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(settingsJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void itemJListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_itemJListValueChanged

        // TODO add your handling code here:
        if (itemJList.getValueIsAdjusting() == false) {
            itemChosen = String.valueOf(itemJList.getSelectedValue());
        }
    }//GEN-LAST:event_itemJListValueChanged

    private void selectJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectJButtonActionPerformed

        // TODO add your handling code here:
        if (itemChosen != null) {

            //
            //outputItem = delegate.getItem(itemChosen);

            //
            this.setVisible(false);
        }
    }//GEN-LAST:event_selectJButtonActionPerformed

    private void closeJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeJButtonActionPerformed

        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_closeJButtonActionPerformed

    private void typeJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeJComboBoxActionPerformed
        // Filter in items of TYPE from delegate
    }//GEN-LAST:event_typeJComboBoxActionPerformed

    private void chanceJSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_chanceJSliderStateChanged

        // TODO add your handling code here:
        dropChance = ((Number) chanceJSlider.getValue()).doubleValue();

        // Change color of text as chance gets lower
        if (dropChance >= 75.0) {
            chanceJTextField.setForeground(new Color(53, 153, 255));
        } else if (dropChance >= 50.0) {
            chanceJTextField.setForeground(Color.GREEN.brighter());
        } else if (dropChance >= 30.0) {
            chanceJTextField.setForeground(Color.YELLOW);
        } else if (dropChance >= 10.0) {
            chanceJTextField.setForeground(Color.ORANGE);
        } else {
            chanceJTextField.setForeground(Color.RED);
        }

        //
        chanceJTextField.setText(chanceJSlider.getValue() + "%");
    }//GEN-LAST:event_chanceJSliderStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonJPanel;
    private javax.swing.JLabel categoryJLabel;
    private javax.swing.JLabel chanceJLabel;
    private javax.swing.JSlider chanceJSlider;
    private javax.swing.JTextField chanceJTextField;
    private javax.swing.JButton closeJButton;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.Box.Filler filler4;
    private javax.swing.JList itemJList;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton selectJButton;
    private javax.swing.JPanel settingsJPanel;
    private javax.swing.JComboBox typeJComboBox;
    // End of variables declaration//GEN-END:variables
}
